import { NameSpace } from 'utils/index';
import exceed from 'utils/apimap';

const { pathResource } = window.FORCE;
const { resourceSubject } = window.FORCE;
const ns = NameSpace('dashboardActivity');
export const RECEIVE_BLAMES = ns('RECEIVE_BLAMES');
export const RECEIVE_LASTCOMMIT = ns('RECEIVE_LASTCOMMIT');

export function getBlame() {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'projects.files.blame',
        params: { projectId: pathResource.id },
        data: {
          ref: resourceSubject.revision,
          file_path: resourceSubject.path,
        },
      })
      .then((result) => {
        dispatch({
          type: RECEIVE_BLAMES,
          data: result,
        });
      });
  };
}

// 获取分支文件最近提交接口
// 请求参数：
// sha - 分支名或版本号
// filepath - 文件相对路径
export function getLastCommit() {
  return (dispatch) => {
    const sha = resourceSubject.revision;
    if (!sha) {
      return;
    }
    exceed
      .forceFetch({
        api: 'projects.files.last_commit',
        params: { projectId: pathResource.id },
        data: {
          sha,
          filepath: resourceSubject.path,
        },
      })
      .then((result) => {
        dispatch({
          type: RECEIVE_LASTCOMMIT,
          data: result,
        });
      });
  };
}
